<template>
	<view class="route-login">
		<view class="inner">
			<image class="img-cont" src="../../static/images/logo.png" mode=""></image>
		</view>
		<view class="footer">
			<view class="btn" @click="handleLogin">
				创建账号/登录
			</view>
		</view>
		<view class="xieyi">
			<u-checkbox-group v-model="checked">
				<u-checkbox activeColor="#ED2920" shape="circle"></u-checkbox>
			</u-checkbox-group>
			<view class="info">
				同意 <text class="text" @click="handleRoute('/pagesA/xieyi/xieyi?status=1')">《用户政策》</text>和<text
					class="text" @click="handleRoute('/pagesA/xieyi/xieyi?status=2')">《隐私协议》</text>
			</view>
		</view>
		<u-modal :show="showPhone" title="授权手机号" :showCancelButton="true" cancelText='关闭' @cancel="handleClosePhone"
			:showConfirmButton="false">
			<view class="slot-content">
				<button class="phonebtn" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber"
					plain="true">获取</button>
			</view>
		</u-modal>
	</view>
</template>

<script setup>
	import handleCache from "@/utils/cache.js"
	import {
		ref
	} from 'vue'
	import {
		userApi,
		storeApi
	} from '@/api/index.js'
	import {
		onReady,
		onShow,
		onLoad
	} from '@dcloudio/uni-app'
	const checked = ref(false)
	const phone = ref('')
	const showPhone = ref(false)
	const infoData = ref('')
	const backShanghuId = ref('')
	const backPath = ref('')
	const quicksaleId = ref('')
	onShow((referrerInfo) => {
		wx.showModal({
			title: '提示',
			content: referrerInfo.scene.toString(),
			success: function(res) {
				if (res.confirm) {} else if (res.cancel) {}
			}
		})

	})
	onLoad((options) => {
		if (options.shanghuId) {
			backShanghuId.value = Number(options.shanghuId)
		}
		if (options.path) {
			backPath.value = options.path
		}
		if (options.quicksaleId) {
			quicksaleId.value = Number(options.quicksaleId)
		}
	})
	const handleLogin = () => {
		if (!checked.value) {
			uni.showToast({
				icon: 'none',
				title: '请勾选协议'
			})
			return
		}
		uni.getProvider({
			service: 'oauth',
			success: function(res) {
				// console.log(res.provider, 6666)
				let pid = getApp().globalData.shareUserId || ''
				if (~res.provider.indexOf('weixin')) {
					uni.showLoading({
						title: '登录中',
						mask: true
					})
					uni.login({
						provider: 'weixin',
						onlyAuthorize: true,
						success: function(loginRes) {
							userApi.userlogin({
								pid,
								code: loginRes.code,
								info: ''
							}).then((resuser) => {
								let data = resuser.data
								let shanghuid = 1
								if (backShanghuId.value) {
									shanghuid = backShanghuId.value
								}
								if (data.bdstoreid) {
									shanghuid = data.bdstoreid
								}
								let erweimaShanghuId = getApp().globalData
									.erweimaShanghuId
								if (erweimaShanghuId) {
									shanghuid = erweimaShanghuId
								}
								let shareShanghuId = getApp().globalData
									.shareShanghuId
								if (shareShanghuId) {
									shanghuid = shareShanghuId
								}


								if (!data.bdstoreid || backShanghuId.value ||
									erweimaShanghuId) {
									userApi.userstore({
										uid: data.id,
										shanghuid
									}).then((res) => {

									})
									getApp().globalData.shareUserId = ''
									getApp().globalData.erweimaShanghuId = ''
									getApp().globalData.shareShanghuId = ''
								}
								data.shanghuid = shanghuid
								storeApi.storeinfo({
									id: shanghuid
								}).then((res) => {
									uni.hideLoading()
									if (res.code === 0) {
										data.shanghuname = res.data.name
										if (!data.phone) {
											data.shanghuid = shanghuid
											infoData.value = data
											uni.hideLoading()
											showPhone.value = true
										} else {
											handleCache.set('userInfo', data,
												'forever')
											if (backPath.value) {
												let path = backPath.value
												if (quicksaleId.value) {
													path = path + '?id=' +
														quicksaleId.value
												}
												if (path === 'index') {
													uni.switchTab({
														url: '/pages/index/index'
													})
													return
												}
												uni.redirectTo({
													url: path
												})
											} else {
												uni.switchTab({
													url: '/pages/index/index'
												})
											}

										}
									}



								})



							})

						}
					});
				}
			}
		});
	}

	const getPhoneNumber = (res) => {

		userApi.phoneJm({
			iv: res.detail.iv,
			encryptedData: res.detail.encryptedData,
			uid: infoData.value.id
		}, {
			loading: {
				text: '获取中'
			}
		}).then((info) => {
			phone.value = info.data
			storeApi.storeinfo({
				id: infoData.value.shanghuid
			}).then((res) => {
				handleClosePhone()
				let data = infoData.value
				if (res.code === 0) {
					data.shanghuname = res.data.name
					data.phone = phone.value
				}
				handleCache.set('userInfo', data,
					'forever')
				if (backPath.value) {
					if (quicksaleId.value) {
						path = path + '?id=' +
							quicksaleId.value
					}
					if (path === 'index') {
						uni.switchTab({
							url: '/pages/index/index'
						})
						return
					}
					uni.redirectTo({
						url: path
					})
				} else {
					uni.switchTab({
						url: '/pages/index/index'
					})
				}
			})
		})
	}
	const handleClosePhone = () => {
		showPhone.value = false
	}
	const handleRoute = (path) => {
		uni.navigateTo({
			url: path
		})
	}
	const getUserInfo = () => {

	}
</script>

<style lang="scss" scoped>
	.route-login {
		min-height: 100vh;
		background-color: #fff;
		padding-top: 10vh;

		.phonebtn {
			margin-top: 20rpx;
			border: none;
			padding: 0;
			width: 420rpx;
			color: #fff;
			font-size: 32rpx;
			background-color: #ED2920;
			height: 100%;
			display: flex;
			justify-content: center;
			align-items: center;
			border-radius: 60rpx;
		}

		.inner {
			min-height: 200rpx;
			display: flex;
			align-items: center;
			justify-content: center;

			.img-cont {
				display: block;
				width: 180rpx;
				height: 180rpx;
			}
		}

		.footer {
			margin-top: 50rpx;
			padding: 0 32rpx;

			.btn {
				height: 80rpx;
				background-color: #ED2920;
				display: flex;
				align-items: center;
				justify-content: center;
				color: #fff;
				border-radius: 60rpx;
			}
		}

		.xieyi {
			display: flex;
			align-items: center;
			padding-left: 32rpx;
			margin-top: 10rpx;
			font-size: 24rpx;

			.info {
				color: #999;

				.text {
					color: #ED2920;
				}
			}
		}
	}
</style>